//
//  NeedishTableViewController.m
//  iNeedish
//
//  Created by Felipe Navarro V. on 28-10-09.
//  Copyright 2009 deis. All rights reserved.
//

#import "TTNeedTableViewController.h"
#import "NeedHelpsViewDataSource.h"
#import "NeedsViewDataSource.h"

#import "NeedHelpsResultsModel.h"
#import "NeedsModel.h"
#import <QuartzCore/QuartzCore.h>

#import "INDefaultStylesheet.h"

static CGFloat kImageWidth = 57;
static CGFloat kImageHeight = 57;
static const CGFloat kSmallMargin = 6;




@implementation TTNeedTableViewController


@synthesize need_id;

- (void) layoutSubviews {
	//*******
	NSString* remoteImage = @"http://profile.ak.fbcdn.net/v223/35/117/q223792_6978.jpg";		
	TTImageView* _imageView = [[[TTImageView alloc] initWithFrame:CGRectZero] autorelease];
	
	TTStyledTextLabel* label1 = [[[TTStyledTextLabel alloc] initWithFrame:CGRectZero] autorelease];
	
	_imageView.URL = remoteImage;
	
	
	
	CALayer * imglayer = [_imageView layer];
	[imglayer setMasksToBounds:YES];
	[imglayer setCornerRadius:10.0];
	[imglayer setBorderWidth:0.5];
	[imglayer setBorderColor:[TTSTYLEVAR(needPictureUrlBorderColor) CGColor]];
	
	
	//Label DisplayName
	label1.font = TTSTYLEVAR(needSubjectFont);
	
	label1.text = [TTStyledText textFromXHTML:@"displayname" lineBreaks:YES URLs:YES];
	//	label1.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
	label1.textColor = TTSTYLEVAR(needSubjectColor);
	label1.backgroundColor = [UIColor clearColor];
	//	label1.fon
	[label1 sizeToFit];
	
	
	UIColor* black = RGBCOLOR(158, 163, 172);
	
	CGFloat padding = 8;
	CGFloat viewWidth = 320;
	CGFloat viewHeight = 80;
	
	CGFloat x = -1;
	CGFloat y = -1;
	
	CGRect frame = CGRectMake(x, y, viewWidth, viewHeight);
	TTView* view = [[[TTView alloc] initWithFrame:frame] autorelease];
	view.backgroundColor = RGBCOLOR(216, 221, 231);
	view.style = [TTShapeStyle styleWithShape:[TTSpeechBubbleShape shapeWithRadius:0 pointLocation:305
																		pointAngle:170
																		 pointSize:CGSizeMake(20,10)] next:
				  [TTSolidFillStyle styleWithColor:RGBCOLOR(216, 221, 231) next:
				   [TTSolidBorderStyle styleWithColor:black width:1 next:nil]]];
	
	
	_imageView.frame = CGRectMake(padding, padding , kImageWidth, kImageHeight);
	label1.frame = CGRectMake(kImageWidth + padding*2 , 10, 320 - kImageWidth - padding, label1.font.lineHeight);
	
	
	[view addSubview:_imageView];	
	
	[view addSubview:label1];	
	
	//******	
	
	self.tableView.tableHeaderView = view;

}


//- (id)init
//{
//    if ((self = [super init])) {
//        self.title = @"Needish";
//		self.variableHeightRows = YES;
////		self.tableView.backgroundColor = [UIColor clearColor];
//       	self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;		
//			
//		[[NeedHelpsResultsModel alloc] load:TTURLRequestCachePolicyDefault more:NO];
//
//    }
//    
//    return self;
//}



- (id)initWithNeed:(NSString*)need {
	self.hidesBottomBarWhenPushed = YES;
	
	TTLOG(@"we initWithNeed: %@ ", need);
	if (self = [super init]) {
		self.need_id = need;
		self.title = @"Need";
		
//		self.autoresizesForKeyboard = YES;
		self.variableHeightRows = YES;
		self.tableView.backgroundColor = [UIColor whiteColor];
//		[self layoutSubviews];
		
		id<TTTableViewDataSource> ds_helps = [NeedHelpsViewDataSource dataSourceWithItems:nil];
		ds_helps.model = CreateNeedHelpsModelWithCurrentSettings(@"needs", @"view", 20, need);
		
		
		[ds_helps load:TTURLRequestCachePolicyDefault more:YES];	  
		self.dataSource = ds_helps;

//		id<TTTableViewDataSource> ds_all = [NeedsViewDataSource dataSourceWithItems:nil];
//		ds_all.model = CreateNeedModelWithCurrentSettings(@"needs", @"all", 20);
//		
//		[ds_all load:TTURLRequestCachePolicyDefault more:YES];	  
//		self.dataSource = ds_all;
		
		//[[NeedHelpsResultsModel alloc] load:TTURLRequestCachePolicyDefault more:NO];		
       	self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;		
	}
	
	
	return self;
}



////////////////////////////////////////////////////////////////////////////////////
#pragma mark TTViewController

- (UIImage*)imageForError:(NSError*)error
{
    return [UIImage imageNamed:@"Three20.bundle/images/error.png"];
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}


- (void)dealloc {
	[need_id dealloc];
    [super dealloc];
}


@end

